## This is R code for "The Demand for Insurance: Incorporating the Severity of
## Losing Office into the Insurance Model of Judicial Independence"
## Main model

rm(list = ls())
## install.packages("pacman") #load all packages
## remotes::install_github("chrisadolph/simcf") since simcf cannot be directly installed
pacman::p_load(
  tidyverse,this.path,texreg,MASS,AER,ivregEX,ivpack,simcf,arm,stargazer,ivmodel
)
## set working directory
setwd(this.path::here(..=1))
## read data
load("Data/dat.RData")

## Preparation -----------------------------------------------------------------
## a. Select the threats variables
dat <- dat %>%
  mutate(
    threat = threat_cumsum,
    threatS = threatS_cumsum
  )

## b. Specify the directories for table and figure
dir.create("./Table", showWarnings = FALSE)
dir.create("./Figure", showWarnings = FALSE)
dir_table <- "./Table/maintable.tex"
dir_figure <- "./Figure/plot_me.pdf"

## Run everything --------------------------------------------------------------
source("Code/EstimationTableFigure.R")

## double check figure
plot_me

ggsave(plot_me, file=dir_figure)
